package buildrobot_info

import (
	"go_server/game_server/protocol/pb"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
)

func (s *Buildrobot_info_service) S2C_DelAllRobot2BuildLvUp(conn ziface.IConnection, roleid int64, msg *pb.DelAllRobot2BuildLvUpC2S) pb.ErrorCodeManager {
	robot := s.LoadByRoleId(roleid)
	msgS2C := &pb.DelRobot2BuildLvUpS2C{}
	isAnyBuildUpgradeSuc := false
	if robot != nil && robot.BuildData != nil {
		builds := make([]*Cache_buildrobot_info_build_data, 0)
		for _, bv := range robot.BuildData {
			bv.RemoveAllRobot(robot.GetRobotBySerid)
			if bv.CalculateUpgradeTime(robot.TransVolume, robot.GetRobotBySerid, false) {
				isAnyBuildUpgradeSuc = true
			}
			builds = append(builds, bv)
		}
		msgS2C.IdleRobots = robot.Pb_GetIdleRobot()
		msgS2C.Builds = make([]*pb.BuildRobotLvUpData, len(builds))
		for i := 0; i < len(builds); i++ {
			msgS2C.Builds[i] = builds[i].ToPb_BuildRobotLvUpData(robot.GetRobotBySerid)
		}

		defer func() {
			robot.Save_AllData()
		}()
		if isAnyBuildUpgradeSuc {
			Buildrobot_info_serviceObj.S2C_BuildRobotUpgradeSuc(conn, robot)
		}
	}
	if msgS2C != nil {
		zlog.Debugf("ProtocolManager_DelBuildRobotLvUp %v", msgS2C)
		defer func() {
			conn.SendBuffPBMsg(uint32(pb.ProtocolManager_DelBuildRobotLvUp), msgS2C)
		}()
	}
	return pb.ErrorCodeManager_SUCCESS
}
